package com.luyue.zagj.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.luyue.zagj.BaseActivity;
import com.luyue.zagj.MainActivity;
import com.luyue.zagj.R;
import com.luyue.zagj.application.MyApl;
import com.luyue.zagj.utils.CharacterFormatUtil;
import com.luyue.zagj.utils.SPutils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;


public class LoginActivity extends BaseActivity {


    @Bind(R.id.login_ac_ed)
    EditText loginAcEd;
    @Bind(R.id.login_psw_ed)
    EditText loginPswEd;
    @Bind(R.id.login_enter_btn)
    Button loginEnterBtn;
    @Bind(R.id.login_forgotPwd_tv)
    TextView loginForgotPwdTv;
    @Bind(R.id.login_register_tv)
    TextView loginRegisterTv;
    private int code = 0;


    private String phone;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activy);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.login_enter_btn, R.id.login_forgotPwd_tv, R.id.login_register_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_enter_btn:
                checkPsw();
                break;
            case R.id.login_forgotPwd_tv:
                Intent intent = new Intent(this, FindPswPhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.login_register_tv:
                Intent it = new Intent(this, RegisterActivity.class);
                startActivity(it);
                break;
        }
    }

    //验证密码
    private void checkPsw() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView()
                    .getWindowToken(), 0);
        }
        phone = loginAcEd.getText().toString();
        password = loginPswEd.getText().toString();
        CreateObsered createObsered = new CreateObsered().invoke();
        Observable<String> phoneObservable = createObsered.getPhoneObservable();
        Observable<String> pwObservable = createObsered.getPwObservable();

        Observable.combineLatest(phoneObservable, pwObservable, new Func2<String, String, Boolean>() {
            @Override
            public Boolean call(String s, String s2) {
                return checkPhone(s) && checkPassword(s2);
            }
        }).filter(new Func1<Boolean, Boolean>() {
            @Override
            public Boolean call(Boolean aBoolean) {
                return aBoolean;
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                    SPutils.put(LoginActivity.this, MyApl.FIRST_OPEN, true);
                    Intent intent3 = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent3);
                    finish();
            }
        });
    }


    public boolean checkPassword(String password) {
        if (null == password || "".equals(password)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!CharacterFormatUtil.isPassword(password)) {
            Toast.makeText(this, "密码中不能含有特殊字符", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public boolean checkPhone(String phone) {
        if (null == phone || "".equals(phone)) {
            Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!CharacterFormatUtil.isPhoneNumberValid(phone)
                && !CharacterFormatUtil.isPersonIdValid(phone)) {
            Toast.makeText(this, "账号格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        if (code == 0) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            code = 1;
        } else {
            code = 0;
            finish();
        }

    }

    private class CreateObsered {
        private Observable<String> phoneObservable;
        private Observable<String> pwObservable;

        public Observable<String> getPhoneObservable() {
            return phoneObservable;
        }

        public Observable<String> getPwObservable() {
            return pwObservable;
        }

        public CreateObsered invoke() {
            phoneObservable = Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    subscriber.onNext(phone);
                }
            });
            pwObservable = Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    subscriber.onNext(password);
                }
            });
            return this;
        }
    }
}
